﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TL_Cloud.Core.Entities;
using TL_Cloud.Core.Services;
using TL_Cloud.Models.BookShop;
using TL_Cloud.RepositoryInterface.BookShop;
using TL_Cloud.ViewModels.BookShop.BKComment;

namespace TL_Cloud.RepositoryService.BookShop
{
    public class BKCommentRepository : BaseRepository<Bk_Comment>, IBKCommentRepository
    {
        public BKCommentRepository(AppBookShopContext myContext,
       IPropertyMappingContainer propertyMappingContainer) : base(myContext, propertyMappingContainer)
        {
            currentDbSet = _myContext.BkComment;
        }

        public async Task<PaginatedList<Bk_Comment>> GetAllEntitiesAsync(BKCommentQueryParameters parameters)
        {
            var query = currentDbSet.AsQueryable();

            if (!string.IsNullOrEmpty(parameters.BookId))
            {
                string BookId = parameters.BookId.ToLowerInvariant();
                query = query.Where(n => n.BookId.ToLowerInvariant() == BookId);
            }
            if (!string.IsNullOrEmpty(parameters.CommentUserId))
            {
                string CommentUserId = parameters.CommentUserId.ToLowerInvariant();
                query = query.Where(n => n.CommentUserId.ToLowerInvariant() == CommentUserId);
            }


            int count = 0;
            List<Bk_Comment> data = new List<Bk_Comment>();
            if (parameters.isPage)
            {
                count = await query.CountAsync();
                data = await query
                    .Skip(parameters.PageIndex * parameters.PageSize)
                    .Take(parameters.PageSize)
                    .Include(d=>d.bookInfo)
                    .ToListAsync();
            }
            else
            {
                data = await query.ToListAsync();
            }
            return new PaginatedList<Bk_Comment>(parameters.PageIndex, parameters.PageSize, count, data);
        }

    }
}
